package com.invengo.module_check.ui.fragment.firstpos;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;

import com.invengo.library_db.entity.module_check.BookBean;
import com.invengo.library_db.entity.module_check.ShelfBean;
import com.invengo.library_rfid.classify.AnalyzeImpl;
import com.invengo.library_rfid.classify.RFIDCallBackListener;
import com.invengo.library_rfid.entity.TagInfo;
import com.invengo.module_check.data.repsitory.business.BusinessRepsitory;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.RxUtils;

public class FirstViewModel extends BaseViewModel<BusinessRepsitory> {
    public SingleLiveEvent<Boolean> showSave = new SingleLiveEvent<>();
    public RFIDCallBackListener rfidService = new RFIDCallBackListener(AnalyzeImpl.TO_BOOK, tagInfo -> onScanResult(tagInfo), state -> {
        cleanRFIDCacheNotStop();
        isReading.set(state);
    });

    public FirstViewModel(@NonNull Application application) {
        super(application, BusinessRepsitory.getInstance());
        showSave.postValue(false);
        registerRFID(rfidService);
    }
    public void onScanResult(TagInfo tagInfo){
        switch (tagInfo.getRfidModel()) {
            case TID:
                if (tagInfo.getScanModel() == AnalyzeImpl.TO_SHELF) {
//                    getShelfByTid(tagInfo.getTag());
                }
                else
                    getBookByTid(tagInfo.getTag());
                break;
            case EPC:
            case U_DATA:
                if (tagInfo.getScanModel() == AnalyzeImpl.TO_SHELF) {
//                    getShelfByCode(tagInfo.getTag());
                }
                else
                    getBookByCode(tagInfo.getTag());
                break;
        }
    }
    /**
     * 按Code查询书本
     *
     * @param code 标签
     */
    private void getBookByCode(String code) {
        model.getBookByCode(code)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bookBean -> updateNewBook(bookBean));
    }


    /**
     * 按Tid查询书本
     *
     * @param tid 标签
     */
    private void getBookByTid(String tid) {
        model.getBookByTid(tid)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(bookBean -> updateNewBook(bookBean));
    }


    private void updateNewBook(BookBean bean) {
        ui.scanBookEvent.postValue(bean);
    }


    public UIChangeObservable ui = new UIChangeObservable();

    public class UIChangeObservable {
        public SingleLiveEvent<List<ShelfBean>> shelfListEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<List<ShelfBean>> nextEvent = new SingleLiveEvent<>();
        public SingleLiveEvent<BookBean> scanBookEvent = new SingleLiveEvent<>();
    }

    public List<ShelfBean> shelfBeans;

    public void getShelfList(String shelfCode) {
        model.getShelfList(shelfCode)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(shelfBeans -> {
                    this.shelfBeans = shelfBeans;
                    showSave.postValue(true);
                    ui.shelfListEvent.postValue(shelfBeans);
                });
    }


    public BindingCommand savenCommand = new BindingCommand(() -> {
        for (ShelfBean shelfBean : shelfBeans) {
            KLog.e("图书名字：" + shelfBean.getFirstBookName() + " 图书barcode：" + shelfBean.getFirstBookID() + " 索书号: " + shelfBean.getBookIndex());

        }
        updateFistPos();
    });

    private void updateFistPos() {
        model.updateFirstBook(shelfBeans)
                .compose(RxUtils.applySchedulersToast(getLifecycleProvider(), this))
                .subscribe(updateResult -> {
                    ui.nextEvent.postValue(shelfBeans);
                });
    }

}
